package com.situ.library.controller;

import com.situ.library.pojo.entity.Book;
import com.situ.library.pojo.entity.Card;
import com.situ.library.pojo.entity.Record;
import com.situ.library.pojo.entity.User;
import com.situ.library.pojo.query.RecordQuery;
import com.situ.library.service.IBookService;
import com.situ.library.service.ICardService;
import com.situ.library.service.IRecordService;
import com.situ.library.service.IUserService;
import com.situ.library.service.impl.BookServiceImpl;
import com.situ.library.service.impl.CardServiceImpl;
import com.situ.library.service.impl.RecordServiceImpl;
import com.situ.library.service.impl.UserServiceImpl;
import com.situ.library.util.MyBeanUtils;
import com.situ.library.util.PageInfo;
import com.situ.library.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/record")
public class RecordController extends BaseController {
    private IRecordService recordService = new RecordServiceImpl();
    private IBookService bookService = new BookServiceImpl();
    private ICardService cardService = new CardServiceImpl();
    private IUserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "selectById":
                selectById(req, resp);
                break;
            case "update":
                update(req, resp);
                break;
            case "updateStatus":
                updateStatus(req, resp);
                break;
        }
    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("RecordController.updateStatus");
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        recordService.updateStatus(Integer.parseInt(id), Integer.parseInt(status));
        toJSON(resp, Result.ok("更新成功"));
    }



    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("RecordServlet.update");
        //Map<String, String[]> parameterMap = req.getParameterMap();
        //id: 1
        //name: zhangsan
        //content: shdishd
        /*String id = req.getParameter("id");
        String name = req.getParameter("name");
        String content = req.getParameter("content");
        Record record = new Record();
        record.setName(name);
        record.setId(Integer.parseInt(id));
        record.setContent(content);*/
        //Record record = MyBeanUtils.copyToBean1(req, Record.class);
        Record record = MyBeanUtils.copyToBean(req, Record.class);
        recordService.update(record);
        //JSONUtils.toJSON(resp, Result.ok("更新成功"));
        toJSON(resp, Result.ok("更新成功"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("RecordServlet.selectById");
        String id = req.getParameter("id");
        Record record = recordService.selectById(Integer.parseInt(id));
        List<Book> list1 = bookService.selectAll();
        List<Card> list2 = cardService.selectAll();
        Map<String, Object> map = new HashMap<>();
        map.put("record", record);
        map.put("book", list1);
        map.put("card", list2);
        toJSON(resp, Result.ok(map));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("RecordServlet.add");
        /*String name = req.getParameter("name");
        String content = req.getParameter("content");
        Record record = new Record();
        record.setName(name);
        record.setContent(content);*/
        Record record = MyBeanUtils.copyToBean(req, Record.class);
        recordService.add(record);
        //JSONUtils.toJSON(resp, Result.ok("添加成功"));
        toJSON(resp, Result.ok("添加成功"));
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("RecordServlet.selectByPage");
        RecordQuery recordQuery = MyBeanUtils.copyToBean(req, RecordQuery.class);
        System.out.println(recordQuery);
        PageInfo pageInfo = recordService.selectByPage(recordQuery);

        //JSONUtils.toJSON(resp, pageInfo);
        toJSON(resp, pageInfo);
    }

}